import axios from "axios";
import { ElLoading } from "element-plus";
import { ElMessage } from 'element-plus'
import store from "../store/index"
let loadingObj = null
const Service = axios.create({
  timeout: 8000,
  baseURL: "http://127.0.0.1:8888/api/private/v1/",
  headers: {
    "Content-Type": "application/json;charset=utf-8",
    "Authorization": store.state.uInfo.userInfo.token
    // "Authorization":
    //  "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE3MTc5MTAyODksImV4cCI6MTcxNzk5NjY4OX0.SUFeLMjBIyRryw5Get7GZt_6XiRZB2OtVBrjH_FhPDs"

  }
})

// 请求拦截-增加loading,对请求做统一处理
Service.interceptors.request.use(config => {
  loadingObj = ElLoading.service({
    lock: true,
    text: "Loading",
    background: 'rgba(0,0,0,0.7)'
  })
  return config
})

// 相应拦截-对返回值做统一处理
Service.interceptors.response.use(response => {

  // response无论如何都要进行关闭
  loadingObj.close()
  const data = response.data
  if (data.meta.status != 200) {
    ElMessage({
      message: data.meta.msg || "服务器出错",
      type: 'error',
      plain: true,
    })
    return data
  }
  return data
}), error => {
  loadingObj.close()
  ElMessage({
    message: "服务器错误",
    type: 'error',
    duration: 2000
  })
};
// get请求
export const get = config => {
  return Service({
    ...config,
    method: "get",
    params: config.data
  })
}
// post请求
export const post = config => {
  return Service({
    ...config,
    method: "post",
    data: config.data
  })
}
// put请求
export const put = config => {
  return Service({
    ...config,
    method: "put",
    data: config.data
  })
}

// delete请求
export const reqdelete = config => {
  return Service({
    ...config,
    method: "delete",
    data: config.data
  })
}
